<template>
  <div>
    <div>
      <el-form :inline="true" :model="agencyType" class="demo-form-inline">
        <el-form-item label="机构类型名">
          <el-input  v-model="agencyType.name" placeholder="请输入机构类型名"></el-input>
        </el-form-item>
        <el-form-item label="机构类型编号">
          <el-input v-model="agencyType.id" placeholder="请输入机构类型编号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="show">查询</el-button>
          <el-button type="primary" @click="clear">清空</el-button>
        </el-form-item>

        <el-form-item style="float: left">
          <el-button type="primary" @click="add">新增</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="danger" @click="deleteByIds">批量删除选中</el-button>
        </el-form-item>

      </el-form>

      <div >
        <el-table
            max-height="400"
            highlight-current-row
            ref="multipleTable"
            tooltip-effect="dark"
            border
            @selection-change="handleSelectionChange"
            :data="tableData"
            style="margin: 0 auto; width: 100%;"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="name" label="机构类型名" header-align="center" align="center"></el-table-column>
          <el-table-column label="操作" width="400" header-align="center" align="center">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="primary"
                  plain
                  @click="updateInfo(scope.row.id)"
              >编辑
              </el-button>

              <el-button
                  size="mini"
                  type="danger"
                  plain
                  @click="deleteById(scope.row.id)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <el-dialog title="信息" :visible.sync="dialogVisible" :before-close="close">
        <el-form :model="agencyType" size="mini">
          <el-form-item label="机构类型名" :label-width="formLabelWidth">
            <el-input v-model="agencyType.name" placeholder="请输入机构类型名" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false;clear()">取 消</el-button>
          <el-button type="primary" @click="addForm()">确 定</el-button>
        </div>
      </el-dialog>


    </div>
    <br>
    <!--分页工具条-->
    <el-pagination
        style="position:absolute;bottom: 20px;margin-left: 40vh"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :background="background"
        :current-page="currentPage"
        :page-sizes="[5, 10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
    </el-pagination>
  </div>


</template>
<script>


import {addAgencyType, changeAgencyType, deleteAgencyType, getAllAgencyType} from "@/api/agencyType";

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 表格数据
      tableData: [],
      addOrUpdate: true,//true为add
      agencyType: {},
      form: {},
      posts: [],
      multipleSelection: [],
      selectedIds: [],
      agencyTypeList: [],
      formLabelWidth: "90px"
    }
  },
  methods: {
    show() {
      getAllAgencyType(this.agencyType, this.currentPage, this.pageSize).then((res) => {
        if (res.data.code === 1) {
          this.totalCount = res.data.data.total;
          this.tableData = res.data.data.rows;
          //console.log(this.tableData)
          if(this.tableData.length===0&&this.currentPage>1){
            this.currentPage-=1;
            this.show();
          }
        }
      }).catch((e) => {
      })
    },

    clear() {
      this.agencyType = {};
      this.show();
    },
    add() {
      this.agencyType={};
      this.addOrUpdate = true;
      this.dialogVisible = true;
    },
    addForm() {
      if (this.addOrUpdate) {
        addAgencyType(this.agencyType).then(res => {
          if (res.data.code === 1) {
            this.$message.success("保存成功")
            this.dialogVisible = false;
            this.show();
          } else {
            this.$message.error(res.data.msg)
          }
        }).catch(e => {
        });
      } else {
        //更新
        changeAgencyType(this.agencyType).then(res => {
          console.log(res)
          if (res.data.code === 1) {
            this.$message.success("更新成功")
            this.dialogVisible = false;
            this.show()
          }
        }).catch(e => {
        });
      }

    },
    close() {
      this.dialogVisible = false;
      this.agencyType = {}
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    updateInfo(id) {
      this.addOrUpdate = false;
      this.agencyType = this.tableData.filter(data => data.id == id)[0];
      this.dialogVisible = true;
    },
    deleteById(id) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(this.tableData.filter(data => data.id === id)[0]);
      this.deleteByIds()
    },
    deleteByIds() {
      // 弹出确认提示框
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //用户点击确认按钮
        //1. 创建id数组, 从 this.multipleSelection 获取即可
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.selectedIds[i] = this.multipleSelection[i].id;
        }
        //console.log(this.selectedIds);
        deleteAgencyType(this.selectedIds).then((resp) => {
          if (resp.data.code == "1") {
            //删除成功
            this.$message.success("恭喜你，删除成功");
            this.clear();
          } else {
            this.$message.error(resp.data.msg);
          }
        }).catch(() => {
        });

      }).catch(() => {
        //用户点击取消按钮
        this.$message.info("已取消删除");
      });

    },

    //分页
    handleSizeChange(val) {
      // 重新设置每页显示的条数
      this.pageSize = val;
      this.show();
    },
    handleCurrentChange(val) {
      // 重新设置当前页码
      this.currentPage = val;
      this.show();
    },


  },
  mounted() {
    this.show();
  },
};
</script>


<style>
* {
  margin: 0;
  padding: 0;
}

.el-header {
  background-color: rgb(238, 241, 246);
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: white;
}

</style>
<script setup>
</script>
